//
//  ANEditorInfoViewController.m
//  Ants
//
//  Created by ZHM on 16/4/26.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANEditorInfoViewController.h"

#import "ANTableViewProfileCell.h"
#import "ANProfieleHeader.h"
#import "ANProfileBaseItem.h"


@interface ANEditorInfoViewController ()


@property (weak, nonatomic) IBOutlet UITextField *textFieldValue;

@end

@implementation ANEditorInfoViewController


- (instancetype)init {
    if (self = [super init]){
        UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"editorInfo" bundle:nil];
        self = [storyboard instantiateInitialViewController];
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)setUpNav {
    
    self.title = self.item.tilte;
    self.textFieldValue.text = self.item.subTilte;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNav];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ANLog(@"选择了保存");
    if (indexPath.section == 1 ) {
        if (self.textFieldValue.text.length == 0) {
            return;
        }
        if ([self.item.tilte isEqualToString:@"手机"]) {
            ANLog(@"选择了保存手机");
            [sharedWebService updatePhoneNumber:self.textFieldValue.text completion:^(BOOL succeeded, NSError *error) {
                if (!error) {
                    [SVProgressHUD showSuccessWithStatus:@"修改成功!"];
                    self.item.subTilte = self.textFieldValue.text;
                    [self popViewVC];
                } else {
                    [SVProgressHUD showErrorWithStatus:@"修改失败!"];
                    [self popViewVC];
                }
            }];
        }else if ([self.item.tilte isEqualToString:@"昵称"]){
            ANLog(@"选择了保存昵称");
            [sharedWebService updateNickName:self.textFieldValue.text completion:^(BOOL succeeded, NSError *error) {
                if (!error) {
                    [SVProgressHUD showSuccessWithStatus:@"修改成功!"];
                    self.item.subTilte = self.textFieldValue.text;
                    [self popViewVC];
                } else {
                    [SVProgressHUD showErrorWithStatus:@"修改失败!"];
                    [self popViewVC];
                }
            }];
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (void)popViewVC {
    [self.navigationController popViewControllerAnimated:YES];
    if ([self.delegater respondsToSelector:@selector(editControllerdidFinishSave:)]) {
        [self.delegater editControllerdidFinishSave:self];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc {
   // NSLog(@"%s",__func__);
}
@end
